﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ExcelAutomator.Configuration;
using ExcelAutomator.Validation;
using NUnit.Framework;

namespace ExcelAutomator.Tests.IntegrationTests
{
	[TestFixture]
	public class ParameterPatternTestFixture : ExcelTestFixtureBase
	{
		#region InvalidParameterId
		[Test]
		public void InvalidParameterId()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName
				},
				ExecutionSets = new List<ExecutionSetConfiguration>() 
				{ 
					new ExecutionSetConfiguration() { 
					Id = "My execution set", 
					Executors = new List<ExecutorConfigurationWrapper>()
					            	{
					            		new ExecutorConfigurationWrapper(
														"range", 
														Constants.ExecutorExtensionsNamespace, 
														string.Format(@"<range action='copy' sourceAddress='Sheet1!A2:%InvalidParameterName' targetAddress='Sheet2!B1' xmlns='{0}'/>", Constants.ExecutorExtensionsNamespace))
					            	} } 
				}
			};
			var sut = new ExcelAutomationService(cfg);
			ValidationReport report;
			sut.Validate(out report);

			Assert.That(report.IsValid, Is.False);
			Assert.That(report.Violations.Count(), Is.EqualTo(1));

		}
		#endregion

		#region LastUsedRowParameter_WithSourceAddressAttribute
		[Test]
		public void LastUsedRowParameter_WithSourceAddressAttribute()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\Book_source.xlsx"
				},
				ExecutionSets = new List<ExecutionSetConfiguration>()
			          		                	{
			          		                		new ExecutionSetConfiguration()
			          		                			{
			          		                				Id = "My Execution Set",
			          		                				Executors = new List<ExecutorConfigurationWrapper>()
			          		                				            	{
			          		                				            		new ExecutorConfigurationWrapper(
			          		                				            			"range",
			          		                				            			Constants.ExecutorExtensionsNamespace,
			          		                				            			string.Format(
			          		                				            				@"<range action='copy' sourceAddress='Sheet1!A1:A%lastUsedRow' targetAddress='Sheet2!B1' xmlns='{0}'/>",
			          		                				            				Constants.ExecutorExtensionsNamespace))
			          		                				            	}
			          		                			}
			          		                	}
			};
			var sut = new ExcelAutomationService(cfg);

			sut.ExecuteAutomation();

			base.StartExcel();
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "B1", x1 => Assert.That(x1, Is.EqualTo("TestData1")));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "B5", x1 => Assert.That(x1, Is.EqualTo("TestData5")));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "B10", x1 => Assert.That(x1, Is.EqualTo("TestData10")));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "B15", x1 => Assert.That(x1, Is.EqualTo("TestData15")));
			base.QuitExcel();
		}
		#endregion

		#region LastUsedRowParameter_WithTargetAddressAttribute
		[Test]
		public void LastUsedRowParameter_WithTargetAddressAttribute()
		{
			var cfg = new ExcelAutomatorConfiguration()
			          	{
			          		Settings = new SettingsConfiguration()
			          		           	{
			          		           		Input = @"Input\Book_source.xlsx"
			          		           	},
			          		ExecutionSets = new List<ExecutionSetConfiguration>()
			          		                	{
			          		                		new ExecutionSetConfiguration()
			          		                			{
			          		                				Id = "My Execution Set",
			          		                				Executors = new List<ExecutorConfigurationWrapper>()
			          		                				            	{
			          		                				            		new ExecutorConfigurationWrapper(
			          		                				            			"range",
			          		                				            			Constants.ExecutorExtensionsNamespace,
			          		                				            			string.Format(
			          		                				            				@"<range action='copy' sourceAddress='Sheet1!A1:A5' targetAddress='Sheet2!B%lastUsedRow' xmlns='{0}'/>",
			          		                				            				Constants.ExecutorExtensionsNamespace))
			          		                				            	}
			          		                			}
			          		                	}
			          	};
			
			var sut = new ExcelAutomationService(cfg);
			sut.ExecuteAutomation();

			base.StartExcel();
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "B1", x1 => Assert.That(x1, Is.EqualTo("TestData1")));
			ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "B5", x1 => Assert.That(x1, Is.EqualTo("TestData5")));
			base.QuitExcel();
		}
		#endregion

		#region InputFilename
		[Test]
		public void InputFilename()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName,
					FilenamePattern = "FooBar_%inputFilename.xlsx"
				}
			};

			var sut = new ExcelAutomationService(cfg);

			Assert.That(sut.ExecutionContexts.First().Configuration.FilenamePattern, Is.EqualTo("FooBar_Book_source.xlsx"));


		}
		#endregion

		#region Date
		[Test]
		public void Date()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName,
					FilenamePattern = "FooBar_%date.xlsx"
				}
			};
		  
		  var sut = new ExcelAutomationService(cfg);

		  var newFilename = string.Format("FooBar_{0}.xlsx", DateTime.Today.ToString("yyyyMMdd"));

		  Assert.That(sut.ExecutionContexts.First().Configuration.FilenamePattern, Is.EqualTo(newFilename));

		}
		#endregion

		#region TargetFilename
		[Test]
		public void TargetFilename()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName,
					Target = @"Target\" + TargetWbName,
					FilenamePattern = "FooBar_%targetFilename.xlsx"
				}
			};

			var sut = new ExcelAutomationService(cfg);

			var filename = string.Format("FooBar_{0}.xlsx", "Book_target");

			Assert.That(sut.ExecutionContexts.First().Configuration.FilenamePattern, Is.EqualTo(filename));
		}
		#endregion

		#region IgnoreExcelSpecificParameterIdsForFilenamePattern
		[Test]
		public void IgnoreExcelSpecificParameterIdsForFilenamePattern()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName,
					Target = @"Target\" + TargetWbName,
					FilenamePattern = "FooBar_%lastUsedRow.xlsx" //Bang this is a Excel Parameter and should be ignored!
				}
			};

			var sut = new ExcelAutomationService(cfg);

			const string filename = "FooBar_%lastUsedRow.xlsx";

			Assert.That(sut.ExecutionContexts.First().Configuration.FilenamePattern, Is.EqualTo(filename));
		}
		#endregion
		
		
		

	}
}
